<?php
/**
 * Zhub - A PHP Framework For API Developer
 *
 * This file is part of the Zhub package.
 *
 * (c) YiLong Fan <fantiq@163.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author fanyilong <fantiq@163.com>
 * @copyright 2017 fanyilong. All rights reserved.
 * @package Zhub
 * @date 18-3-7
 */

namespace Zhub\DataBase\Grammar\MySQL;


use Zhub\DataBase\Grammar\Grammar;

class Update extends Grammar
{
    const SET = 0b0;

    const WHERE = 0b1;

    public function set(string $field, $value):self
    {
        $this->sqlComponent[self::SET][] = "$field=:$field";
        $this->bindValue(":$field", $value);
        return $this;
    }

    public function increment(string $field, int $step = 1):self
    {
        $this->ration($field, $step, '+');
        return $this;
    }

    public function decrement(string $field, int $step = 1):self
    {
        $this->ration($field, $step, '-');
        return $this;
    }

    private function ration($field, $step, $ration)
    {
        $this->sqlComponent[self::SET][] = $field. '='. $field . $ration . $step;
    }

    public function where()
    {}

    public function toSQL(): string
    {
        $set = $this->sqlComponent[self::SET];
        if (empty($set)) {
            //
        }
        $set = implode(',', $set);
        $where = $this->sqlComponent[self::WHERE];
        $where = empty($where) ? '' : $where;

        return sprintf("UPDATE %s SET %s %s",
            $this->tableName, $set, $where);
    }

    public function initialize()
    {
        parent::initialize();

        $this->sqlComponent = [
            self::SET => [],
            self::WHERE => null,
        ];
    }
}